package main

import (
	"github.com/gin-gonic/gin"
	"github.com/micro/go-micro/registry"
	"github.com/micro/go-micro/registry/etcd"
	"github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/web"
	"microprj/lib/log"

	"microprj/lib/auth/token-jwt"
	"net/http"
	"time"
)

var NAME = "go.micro.api.login"

func init() {
	log.InitZap()
}

func main() {

	reg := etcd.NewRegistry(registry.Addrs("127.0.0.1:2379"))

	server := web.NewService(
		web.Name(NAME),
		web.Registry(reg),
		web.RegisterTTL(time.Second*30),
		web.RegisterInterval(time.Second*10),
	)
	server.Handle("/", GetRouter())
	if err := server.Run(); err != nil {

	}
}

func GetRouter() http.Handler {
	router := gin.Default()
	r := router.Group("/login")
	r.GET("/token", Auth)
	//r.POST("/auth",Auth)
	return router
}

func Auth(c *gin.Context) {
	var (
		auth string
		err  error
		msg  string
	)
	token := token_jwt.Token{}
	if auth, err = token.Encode("11", "bogerj", time.Now().Add(time.Hour * 24 * 3).Unix()); err != nil {
		logger.Error("auth error:", err)
		msg = err.Error()
	}
	c.JSON(200, gin.H{
		"message": msg,
		"token":   auth,
	})
}
